<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form :form="config" layout="inline">
        <a-row :gutter="24">
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="公众号前端版本号" :labelCol="{ style: 'width: 150px' }">
              <a-input placeholder="请输入" v-model="config.wechatVersion"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label=" " :colon="false" :labelCol="{ style: 'width: 150px' }">
              <a-button @click="refreshMenu" type="primary">更新公众号菜单</a-button>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="微信v3平台证书" :labelCol="{ style: 'width: 150px' }">
              <a-input placeholder="请输入" v-model="config.wechatCertificate"></a-input>
            </a-form-item>
          </a-col>
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="证书过期时间" :labelCol="{ style: 'width: 150px' }">
              <a-input placeholder="请输入" v-model="config.certificateExpireTime"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label=" " :colon="false" :labelCol="{ style: 'width: 150px' }">
              <a-button @click="downloadCertNumber" type="primary">下载证书序列号</a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="用户提现设置" :labelCol="{ style: 'width: 150px' }">
              <j-dict-select-tag v-model="config.userWithdrawal" placeholder="请选择" dictCode="userWithdrawal" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="配置信息" :labelCol="{ style: 'width: 150px' }">
              <a-input disabled v-model="config.configInfo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="最近执行日期" :labelCol="{ style: 'width: 150px' }">
              <a-input placeholder="请输入" disabled v-model="config.lastExecutionDate"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="执行天数（天）" :labelCol="{ style: 'width: 150px' }">
              <a-input placeholder="请输入" v-model="config.executionDays"></a-input>
            </a-form-item>
          </a-col>
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label="执行整点（时）" :labelCol="{ style: 'width: 150px' }">
              <a-time-picker class="per100" v-model="config.executionTime" format="HH:mm" valueFormat="HH:mm" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :sm="24" :md="12" :xl="8">
            <a-form-item label=" " :colon="false" :labelCol="{ style: 'width: 150px' }">
              <a-button type="primary" @click="save" html-type="submit">保存</a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <a-divider />

      <div>
        <a-form-model  layout="inline" :rules="validatorRules" :model="queryParam" ref="myForm">
          <a-row :gutter="24" >
            <a-col :span="15">
              <a-form-model-item label="区域" prop="businessId">
                <a-row style="width: 100%" :gutter="10" type="flex">
                  <a-col :span="5">
                    <a-select
                      placeholder="请选择省"
                      v-model="queryParam.provinceCode"
                      @change="e => addressChange(e, 'cityOptions', 'province')"
                    >
                      <a-select-option v-for="item in provinceOptions" :key="item.name" :value="item.code">
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-col>
                  <a-col :span="5">
                    <a-select
                      placeholder="请选择市"
                      v-model="queryParam.cityCode"
                      @change="e => addressChange(e, 'areaOptions', 'city')"
                    >
                      <a-select-option v-for="item in cityOptions" :key="item.name" :value="item.code">
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-col>
                  <a-col :span="5">
                    <a-select
                      placeholder="请选择区"
                      v-model="queryParam.areaCode"
                      @change="e => addressChange(e, 'streetOptions', 'area')"
                    >
                      <a-select-option v-for="item in areaOptions" :key="item.name" :value="item.code">
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-col>
                  <a-col :span="5">
                    <a-select
                      placeholder="请选择街道"
                      v-model="queryParam.streetCode"
                      @change="e => addressChange(e, '', 'street')"
                    >
                      <a-select-option v-for="item in streetOptions" :key="item.name" :value="item.code">
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-col>
                  <a-col :span="4">
                    <a-select
                      placeholder="请选择商家"
                      v-model="queryParam.businessId"
                      @change="businessChange"
                    >
                      <a-select-option v-for="item in agentOptions" :key="item.name" :value="item.code">
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-col>
                </a-row>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-model-item label="统计日期" prop="dateRange">
                <a-range-picker allowClear class="per100" v-model="queryParam.dateRange" @change="timeChange"/>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24" style="height: 55px">
            <a-col :span="3">
              <a-button type="primary" @click="generateReport">数据生成</a-button>
            </a-col>
          </a-row>
        </a-form-model>

        <div style="width: 100%;height: 100px;background: lightblue;border: 1px solid lightblue;border-radius: 3px;">
          <a-row type="flex" justify="center" align="middle" style="height: 100px;">
            <a-col :span="2">
                <a-icon style="display: block;" type="bulb" />
            </a-col>
            <a-col :span="22">
              <h3>提示信息：</h3>
              <h6>1."区域"必须选择到具体代理商，才可生成该代理商的经营统计数据</h6>
              <h6>1."区域"不选，默认生成全部代理商的经营统计数据(请谨慎点击)</h6>
            </a-col>
          </a-row>
        </div>
      </div>
    </div>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import Api from '@/api/system/System'
import api from '@api/statisticsManage/statistics'
import { getAction, getFileFromHttpUrl, putAction } from '@api/manage'
import CommonApi from '@api/Common'

export default {
  name: 'SystemManage',
  mixins: [JeecgListMixin],
  data() {
    return {
      config: {
        userWithdrawal: undefined
      },
      url: {
        menu: Api.menu,
        query: Api.query,
        certNumber: Api.certNumber,
        edit: Api.edit,
        exportXlsUrl: api.exportAdmin
      },
      queryParam:{},
      provinceOptions: [],
      cityOptions: [],
      areaOptions: [],
      streetOptions: [],
      agentOptions: [],
      validatorRules: {
        businessId: [{ required: true, message: '商家不能为空', trigger: 'change' }],
        dateRange: [{ required: true, message: '时间不能为空', trigger: 'change' }],
      }
    }
  },
  created() {
    this.loadData()
    this.getAddress('provinceOptions')
  },
  methods: {
    loadData() {
      getAction(this.url.query).then(res => {
        if (res.code === 200) {
          this.config = res.result
        }
      })
    },
    businessChange(e) {
      this.agentChange(e, 'networkOptions', 'agentCode')
      this.$refs.myForm.validateField('businessId')
    },
    // 获取地址
    getAddress(name, data) {
      getAction(CommonApi.addressList, data).then(res => {
        this[name] = res.result
      })
    },
    timeChange(e, arr) {
      if (arr[0]) {
        this.queryParam.startTime = arr[0]
        this.queryParam.endTime = arr[1]
      } else {
        this.queryParam.startTime = undefined
        this.queryParam.endTime = undefined
      }
    },
    generateReport(){
      this.$refs.myForm.validate(valid => {
        if (valid) {
          this.handleExportXls('系统数据', { dateRange: undefined, periodType: 'day' })
        }
      })
    },
    refreshMenu() {
      getAction(this.url.menu).then(res => {
        if (res.code === 200) {
          this.$message.success('更新公众号菜单成功')
        } else {
          this.$message.error('更新公众号菜单失败')
        }
      })
    },
    downloadCertNumber() {
      getFileFromHttpUrl(this.url.certNumber)
    },
    save() {
      putAction(this.url.edit, this.config).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style scoped></style>

